package com.zhu.oj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhu.oj.common.annotation.AuthCheck;
import com.zhu.oj.common.BaseResponse;
import com.zhu.oj.common.enums.ErrorCode;
import com.zhu.oj.common.utils.ResultUtils;
import com.zhu.oj.common.constant.UserConstant;
import com.zhu.oj.common.exception.BusinessException;

import com.zhu.oj.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.zhu.oj.model.dto.questionsubmit.QuestionSubmitQueryRequest;
import com.zhu.oj.model.entity.QuestionSubmit;
import com.zhu.oj.model.entity.User;
import com.zhu.oj.model.vo.EcharsVo;
import com.zhu.oj.model.vo.QuestionSubmitVO;
import com.zhu.oj.service.QuestionSubmitService;

import com.zhu.oj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 题目提交接口
 *
 *
 */
@RestController
@RequestMapping("/question_submit")
@Slf4j
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    /**
     * 题目提交
     *
     * @param questionSubmitAddRequest
     * @param request
     * @return
     */
    @PostMapping("/")
    public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,
            HttpServletRequest request) {
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能提交
        final User loginUser = userService.getLoginUser(request);
        long postId = questionSubmitAddRequest.getQuestionId();
        long result = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        return ResultUtils.success(result);
    }
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.DEFAULT_ROLE)
    public BaseResponse<Page<QuestionSubmitVO>> listQuestionByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest, HttpServletRequest httpServletRequest) {
        long current = questionSubmitQueryRequest.getCurrent();
        long size = questionSubmitQueryRequest.getPageSize();
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, size),
                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        User loginUser = userService.getLoginUser(httpServletRequest);
        return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage,loginUser));
    }


    @GetMapping("/getEcharts")
    public BaseResponse<List<EcharsVo>> getechars() {
        return ResultUtils.success(questionSubmitService.getechars());
    }



}
